package com.gskj.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.gskj.servicebase.exceptionhandler.GuLiException;
import com.gskj.vod.service.IVodService;
import com.gskj.vod.utils.ConstantVodUtil;
import com.gskj.vod.utils.InitVodClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * @author :CodingFarmer_码农  Connor(康纳)_郭
 * @date : 09:53 20.5.19
 * @Company : http://www.gskj.com
 * @Version : 1.0
 */
@Service
public class VodServiceImpl implements IVodService {

    /**
     * 流式上传接口, 上传视频文件流
     *
     * @param file 文件
     * @return
     */
    @Override
    public String uploadVideoAly(MultipartFile file) {

        try {
            // key
            String accessKeyId = ConstantVodUtil.ACCESS_KEY_ID;
            // 密钥
            String accessKeySecret = ConstantVodUtil.ACCESS_KEY_SECRET;
            // 文件原始名称
            String fileName = file.getOriginalFilename();
            // 上传文件后要显示的名称
            String title = fileName.substring(0, fileName.lastIndexOf("."));

            // 上传文件流
            InputStream inputStream = file.getInputStream();

            UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);

            //视频 id
            String videoId = null;

            if (response.isSuccess()) {

                videoId = response.getVideoId();
                //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
            } else {
                videoId = response.getVideoId();
            }
            return videoId;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /***
     *   批量删除 视频
     * @param videoIdList
     */
    @Override
    public void removeBatchVideoById(List videoIdList) {
        try {
            // 初始化对象
            DefaultAcsClient defaultAcsClient = InitVodClient.initVodClient(ConstantVodUtil.ACCESS_KEY_ID, ConstantVodUtil.ACCESS_KEY_SECRET);
            // 创建 删除视频的 request对象
            DeleteVideoRequest request = new DeleteVideoRequest();

            // 通过  StringUtils.join() 转换 list中的数据为 1,2,3,4 ...
            String ids = StringUtils.join(videoIdList.toArray(), ",");

            // 设置要删除的视频 ids
            request.setVideoIds(ids);
            // 调用defaultAcsClient 方法
            defaultAcsClient.getAcsResponse(request);

        } catch (Exception e) {
            e.printStackTrace();
            throw new GuLiException(20001, "删除视频失败");
        }
    }
}
